package com.yxp423.ssmempl.config;

import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.MultipartProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;

/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-07-31 20:20
 * @Version: 1.0
 */
@Configuration
@ConditionalOnClass({Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class})
@ConditionalOnProperty(prefix = "spring.servlet.multipart", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(MultipartProperties.class)
public class WebUploadConfig {


	//	华东机房
	@Bean
	public com.qiniu.storage.Configuration qiniuConfig() {

		return new com.qiniu.storage.Configuration(Zone.zone2());
	}

	/**
	 * 构建七牛上传工具实例
	 *
	 * @return
	 */
	@Bean
	public UploadManager uploadManager() {

		return new UploadManager(qiniuConfig());

	}

	@Value("${qiniu.accesskey}")
	private String accesskey;

	@Value("${qiniu.secretKey}")
	private String secretKey;


	/**
	 * 认证信息实例
	 *
	 * @return
	 */
	@Bean
	public Auth auth() {
		return Auth.create(accesskey, secretKey);
	}

	/**
	 * 空间管理实例
	 *
	 * @return
	 */
	@Bean
	public BucketManager bucketManager() {

		return new BucketManager(auth(), qiniuConfig());
	}

	@Bean
	public Gson gson() {

		return new Gson();
	}
}
